/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import csse3004.GD.DA.LoginDA;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author David Bullock 42062222
 */
public class createUserController extends SimpleFormController {

    protected final Log log = LogFactory.getLog(getClass());
    LoginDA service = new LoginDA();

    @Override
    public ModelAndView onSubmit(Object command) {
        createUser user = (createUser) command;
        Account account = new Account();
        account.setEmail(user.getEmail());
        if (user.getType().equals("Web")) {
            account.setType(Account.AccountType.KeyUser);
            String[] types = {"Web", "Master", "Creator", "Admin", "SysAdmin"};
        } else if (user.getType().equals("Master")) {
            account.setType(Account.AccountType.PM);

        } else if (user.getType().equals("Creator")) {
            account.setType(Account.AccountType.PC);
        } else if (user.getType().equals("Admin")) {
            account.setType(Account.AccountType.PAdmin);
        } else if (user.getType().equals("SysAdmin")) {
            account.setType(Account.AccountType.SysAdmin);
        }
        
            account.setUsername(user.getUserName());
            account.setPassword("timp"); //TODO nasty hack
            account.setUserID(service.getNextID("Users"));
        
        service.createAccount(account);

        ModelAndView modelAndView = new ModelAndView(new RedirectView(getSuccessView()));

        return modelAndView;

    }
}
